package com.haixinga.commbiz.member.web;

import com.haixinga.commbiz.commons.biz.UserKit;
import com.haixinga.commbiz.commons.exception.BizException;
import com.haixinga.commbiz.member.vo.MemberLoginUserVO;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** ctrl拦截器， 统一判断拦截器 */
@Component
public class CheckSelectedCommInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // 判断是否选择的相关业务
        MemberLoginUserVO user = UserKit.getCurrentUser(MemberLoginUserVO.class);
        if(user.getSelectedCmId() == null){
            throw new BizException("获取选择信息失败！");
        }

        return true;
    }


    /** postHandler是在请求结束之后, 视图渲染之前执行的,但只有preHandle方法返回true的时候才会执行
     * 如果ctrl使用了@RestController或者@ResponseBody注解 则ModelAndView = null, 因为不走视图转换器, 而是走的RequestResponseBodyMethodProcessor。
     * ————————————————
     *
     * **/
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        //do
    }

}
